﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

/// <summary>
/// 重连系统
/// </summary>
public class _ReconnectSystem : ECSSystemtBase
{
    private bool isConnect = false;//是否联网
    private int timer = 50;//延迟重连
    public override void OnInit()
    {
        //InputManager.AddListener<InputNetworkConnectStatusEvent>(ReceviceSocketStatus);
    }

    public override void OnDispose()
    {
        //  InputManager.RemoveListener<InputNetworkConnectStatusEvent>(ReceviceSocketStatus);
    }

    public override void FixedUpdate(int delaTime)
    {
        //非本地，非联网，暂停
        if (!MyWorld.IsLocal && !NetworkManager.IsConnect)
        {
            //暂停游戏
            MyWorld.IsStart = false;
        }
    }

    public override void RunByPause()
    {
        //非联网，非本地，非结束
        if (!isConnect && !MyWorld.IsLocal && !MyWorld.IsFinish)
        {
            timer--;
            if (timer < 0)
            {
                timer = 5;
                isConnect = true;
                //重连()
                NetworkManager.Connect();
            }
        }
    }

    private void ReceiveSocketStatue(InputNetworkConnectStatusEvent e)
    {
        isConnect = false;
        if (e.m_status == NetworkState.Connected)
        {
            isConnect = true;

            Debug.Log("主动重连");

            //ApplicationStatusManager.GetStatus<LoginState>().Login(SystemInfo.deviceUniqueIdentifier, UserData.NickName);
        }
    }
}

